package com.kevin.j2eeToolBox.thread;

import java.util.Random;

public class ThreadlocalTest1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		for (int i = 0; i < 2; i++) {
			new Thread() {
				@Override
				public void run() {
					DataSource.getInstance().setDate(new Random().nextInt());
					System.out.println(DataSource.getInstance().getDate());
				}
			}.start();
		}
	}
}

class DataSource {

	private static ThreadLocal<DataSource> threadLocal = new ThreadLocal<DataSource>();

	private int date;

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}

	private DataSource() {

	}

	public static DataSource getInstance() {
		DataSource instance = threadLocal.get();

		if (instance == null) {
			instance = new DataSource();
			threadLocal.set(instance);
		}

		return instance;

	}

}
